package com.tianruan.dc.modules.sys.user.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tianruan.dc.common.utils.IdGen;
import com.tianruan.dc.modules.sys.user.entity.dto.LearnPathDTO;
import com.tianruan.dc.modules.sys.user.mapper.LearnPathMapper;
import com.tianruan.dc.modules.sys.user.service.ILearnPathService;
import com.tianruan.dc.modules.sys.user.utils.UserUtils;
import com.tianruan.dc.modules.userinfo.result.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * @Author: AmiroKD
 * @CreateTime: 2024-07-07
 * @Version: 1.0
 */
@Service
@Slf4j
public class LearnPathServiceImpl implements ILearnPathService {

    @Autowired
    private LearnPathMapper learnPathMapper;

    /**
     * 新增学科学习路线
     * @param learnPathDTO
     */
    @Override
    public void saveLeanPath(LearnPathDTO learnPathDTO) {
        //以UUID为主键
        learnPathDTO.setId(IdGen.uuid());
        //添加时间
        //DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
//        LocalDateTime nowTime = LocalDateTime.now();
//        String now = formatter.format(nowTime);
//        learnPathDTO.setCreateTime(nowTime);
        Date localDate = new Date();
        learnPathDTO.setCreateTime(localDate);
        //添加创作者
        learnPathDTO.setCreateId(UserUtils.getUser().getId());
        //传入数据库
        learnPathMapper.save(learnPathDTO);
    }

    /**
     * 判断是否已经存在此学科
     * @param subject
     * @return
     */
    @Override
    public boolean isSubjectExist(String subject) {
        return learnPathMapper.getIdBySubject(subject) == null;
    }

    @Override
    public String getIdBySubject(String subject) {
        return learnPathMapper.getIdBySubject(subject);
    }

    /**
     * 删除学科学习路线
     * @param id
     */
    @Override
    public void deleteSubject(String id) {
        learnPathMapper.deleteSubject(id);
    }

    /**
     * 查询回显学科学习路线
     * @param learnPathDTO
     * @return
     */
    @Override
    public PageResult selectLearnPath(LearnPathDTO learnPathDTO) {
        //开始分页查询
        PageHelper.startPage(learnPathDTO.getPage(),learnPathDTO.getPageSize());

        Page<LearnPathDTO> page = learnPathMapper.selectLearnPath(learnPathDTO);

        long total = page.getTotal();
        List<LearnPathDTO> records = page.getResult();
        return new PageResult(total,records);
    }

    /**
     * 根据ID查询学习指南
     * @param id
     * @return
     */
    @Override
    public LearnPathDTO selectById(String id) {
        LearnPathDTO learnPathDTO = learnPathMapper.selectById(id);
        return learnPathDTO;
    }

    @Override
    public boolean isSubjectIdExist(String id) {
        return learnPathMapper.getIdBySubjectId(id) == null;
    }

}
